//
//  DiscountViewController.m
//  ShopXinyi
//
//  Created by qianfeng on 15/9/14.
//  Copyright (c) 2015年 qianfeng. All rights reserved.
//

#import "DiscountViewController.h"
#import "DiscountGoodCell.h"
#import "DiscountHeadCell.h"
#import "HttpRequestManager.h"
#import "DiscountGoodsModel.h"
#import "UIImageView+AFNetworking.h"

@interface DiscountViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
{
    NSMutableArray *_dataArray;
}

@property (weak, nonatomic) IBOutlet UICollectionView *myCollectionView;

@end

@implementation DiscountViewController
- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        _dataArray = [NSMutableArray new];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [_myCollectionView registerNib:[UINib nibWithNibName:@"DiscountHeadCell" bundle:nil] forCellWithReuseIdentifier:@"DiscountHeadCell"];
    [_myCollectionView registerNib:[UINib nibWithNibName:@"DiscountGoodCell" bundle:nil] forCellWithReuseIdentifier:@"DiscountGoodCell"];
    [self requestDataFromNet];
}


-(void)requestDataFromNet
{
    [[HttpRequestManager shareInstance]getDiscountGoodsInfoSuccess:^(id responseObj) {
        NSDictionary *dataDic = responseObj;
        NSArray *goodListArray = dataDic[@"godos_list"];
        
        NSDictionary *headDic = [goodListArray firstObject];
        NSDictionary *home1Dic = headDic[@"home1"];
        DiscountGoodsModel *model = [DiscountGoodsModel new];
        model.goodsImage = home1Dic[@"image"];
        [_dataArray addObject:model];
        
        NSDictionary *goodListDic = [goodListArray lastObject];
        NSDictionary *goodsDic = goodListDic[@"goods"];
        NSArray *itemArray = goodsDic[@"item"];
        for (NSDictionary *dic in itemArray) {
            DiscountGoodsModel *model = [DiscountGoodsModel new];
            model.goodsID = dic[@"goods_id"];
            model.goodsImage = dic[@"goods_image"];
            model.goodsMKPrice = dic[@"goods_marketprice"];
            model.goodsName = dic[@"goods_name"];
            model.goodsPMPrice = dic[@"goods_price"];
            model.goodsPrice = dic[@"goods_promotion_price"];
            model.goodsStorage = dic[@"goods_storage"];
            [_dataArray addObject:model];
        }
        [_myCollectionView reloadData];
        
    } failure:^(NSError *error) {
        NSLog(@"获取天天特价数据失败,error==%@",error);
    }];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark -UICollectionViewDataSource
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _dataArray.count;
}
//cell 复用
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    DiscountGoodsModel *model = _dataArray[indexPath.row];
    if (indexPath.row == 0) {
        DiscountHeadCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"DiscountHeadCell" forIndexPath:indexPath];
        [cell.headImgView setImageWithURL:[NSURL URLWithString:model.goodsImage]];
        return cell;
    }else{
        DiscountGoodCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"DiscountGoodCell" forIndexPath:indexPath];
        cell.layer.borderWidth = 1;
        cell.layer.borderColor = (__bridge CGColorRef)([UIColor darkGrayColor]);
        [cell.imgView setImageWithURL:[NSURL URLWithString:model.goodsImage]];
        cell.nameLabel.text = model.goodsName;
        cell.discPrice.text = model.goodsPrice;
        cell.shopPrice.text = model.goodsMKPrice;
        return cell;
    }
    
}

//使cell 能够充满屏幕
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        return CGSizeMake(_myCollectionView.frame.size.width-20, (_myCollectionView.frame.size.width-20)/320.0*170);
    }else
        return CGSizeMake((_myCollectionView.frame.size.width-20)/2.0, (_myCollectionView.frame.size.width-15)/2.0/145*173);
}


@end
